		*********************************************************************
		* 		Health Information, Treatment, and Worker Productivity 	    *
		* 																    *
		*		Authors: Andrew Dillon, Jed Friedman, Pieter Serneels	    *
		*       Code contributed by: Tyler Box, Andrew Dillon, Jed Friedman *
		*							 and Sarah Kopper					    *
		*		Final code edit by: Tyler Box      						    *
		* 		Published in: Journal of the European Economic Association  *
		*		Last Edited: TB, April 6th, 2020 						    *
		* 		STATA Version 15										    *
		*********************************************************************

clear all
set more off
set matsize 10000

cap ssc install tabstatmat
cap ssc install egenmore
cap ssc install xml_tab

timer clear
timer on 1

cd "C:/Users/tjb0217/Box Sync/Tyler/Nigeria Malaria/JEEA/JEEA Public Disclosure - 04062020"
global dir `c(pwd)'
global data "${dir}/data"
global output "${dir}/output"


*********************************
***** SECTION 1 - DATA PREP *****
*********************************


**Table 1: Worker characteristics and balance of characteristics across work groups **
use "$data\balance.dta", clear
	
global depvars "age ln_exp_year edyrs BMI hhsize_p rooms_p n_cattle_p n_poultry_p hhasset_m1D"

foreach i in $depvars {
	sum `i'
	matrix N`i'=r(N)
	matrix m`i'=r(mean)
	matrix sd`i'=r(sd)
	matrix `i'=N`i', m`i', sd`i'
	matrix rownames `i'=`i'
	}

matrix table_1=age\ln_exp_year\edyrs\BMI\hhsize_p\rooms_p\n_cattle_p\n_poultry_p\hhasset_m1D

frmttable using "$output\Table1.doc", replace sdec(0, 2, 2) ///
statmat(table_1) varlabels title("Table 1") coljust(l;c) ctitles("" ,N , Mean, SD)	

eststo clear

// Table A3 in the Appendix - Mean differences in worker socio-economic characteristics between workgroups from Table 1 //
foreach i in $depvars {
reg `i' i.gang 
testparm i.gang
local pvalue_all=r(p)
eststo `i'_2, addscalars(pvalue_all `pvalue_all')
}
xml_tab *_2, save($output\Appendix\TableA1.xls) stats(N pvalue_all) title(T1) sd2 replace sheet(T1) format(SCLR3 NCLR3) ///
	keep(*gang )

		
** Earnings, Hours, Productivity Descriptives **

use "$data\workerbyweek.dta", clear

drop if week_int==. 

collapse wage scrabbler (sum) daysworked scrabbledays, by(workerID)

gen scrabbleratio=scrabbledays/daysworked

**FULL SEASON-15 weeks**

foreach i in wage daysworked scrabbler scrabbleratio {
	sum `i'
	matrix N`i'=r(N)
	matrix m`i'=r(mean)
	matrix sd`i'=r(sd)
	matrix `i'=N`i', m`i', sd`i'
	matrix rownames `i'=`i'
	}

matrix table_1_ext=wage\daysworked\scrabbler\scrabbleratio

frmttable using "$output\Table1.doc", append sdec(0, 2, 2) statmat(table_1_ext) varlabels title("Table 1") coljust(l;c) ctitles("" ,N , Mean, SD)	

**INTERVENTION PERIOD**

use "$data\workerbyweek.dta", clear


drop if week_int==. 
keep if week_earn<7

collapse wage scrabbler (sum) daysworked scrabbledays, by(workerID)

gen scrabbleratio=scrabbledays/daysworked
sum scrabbleratio


foreach i in wage daysworked scrabbleratio {
	sum `i'
	matrix N`i'=r(N)
	matrix m`i'=r(mean)
	matrix sd`i'=r(sd)
	matrix `i'=N`i', m`i', sd`i'
	matrix rownames `i'=`i'
	}

matrix table_1_ext=wage\daysworked\scrabbleratio

frmttable using "$output\Table1.doc", append sdec(0, 2, 2) ///
statmat(table_1_ext) varlabels title("Table 1") coljust(l;c) ctitles("" ,N , Mean, SD)


** Balance tests for work characteristics 
use "$data\workerbyweek.dta", clear

eststo clear

// Table A1 Part A in the Appendix - Mean and standard deviation of weekly earnings, days worked, and daily conditional earnings by week of harvest in study period and by work group for all workers //
foreach i in wage daysworked scrabbleratio {
reg `i' i.gang 
testparm i.gang
local pvalue_all=r(p)
eststo `i'_2, addscalars(pvalue_all `pvalue_all')
}
xml_tab *_2, save($output/Appendix/TableA2A.xls) stats(N pvalue_all) title(T1) sd2 append sheet(T1_R2_b) format(SCLR3 NCLR3) ///
	keep(*gang)

keep if week_earn<7	
	eststo clear

foreach i in wage daysworked scrabbleratio {
reg `i' i.gang 
testparm i.gang
local pvalue_all=r(p)
eststo `i'_2, addscalars(pvalue_all `pvalue_all')
}
xml_tab *_2, save($output/Appendix/TableA2A.xls) stats(N pvalue_all) title(T1) sd2 append sheet(T1_R2_c) format(SCLR3 NCLR3) ///
	keep(*gang)

****************************************************
** Table 2: Descriptives for worker labor outcomes**
****************************************************

use "$data\dailydata", clear

eststo clear

gen daysworked=attendance
gen weeklyearn=total_amount
collapse (sum) weeklyearn scrabbledays daysworked (mean) total_amount attendance, by(week_earn gang workerID)

gen wage=total_amount/attendance

egen tag_ww=tag(week_earn workerID)

keep if week_earn<7
tabstat weeklyearn daysworked wage scrabbledays if tag_ww==1, by(week_earn) stat(N sum mean sd) format(%9.0fc) save
tabstatmat A
matrix TAB_1=A'
xml_tab TAB_1, save($output/Appendix/TableA2A.xls)  title(Work characteristics--Weekly All ) replace format(SCLR3 NCLR3) 

// Table A1 Part B in the Appendix
tabstat weeklyearn daysworked wage scrabbledays if tag_ww==1, by(gang) stat(N sum mean sd) format(%9.0fc) save
tabstatmat B
matrix TAB_2=B'
xml_tab TAB_2, save($output/Appendix/TableA2B.xls)  title(Work characteristics--Weekly All ) replace format(SCLR3 NCLR3)

******************************
**Table 3:  Parasite Density**
******************************

use "$data\workerbyweek.dta", clear
collapse para_max mal_pos (sum) daysworked, by(workerID)
keep if daysworked>0

tabout mal_pos para_max using "$output\Table3.xls", layout(col) cells(freq cell) format(0 2) replace


***********************************************
**Table 1 continued:  Balance tests within work groups**
***********************************************

use "$data\balance.dta", clear

global depvars "age ln_exp_year edyrs BMI hhsize_p rooms_p n_cattle_p n_poultry_p hhasset_m1D"

eststo clear
foreach i in $depvars {
reg `i' i.week_int if gang==1  
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 1
local means_all = r(mean)
di "`means_all'"
eststo `i'_1, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_1 , save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G1) sd2 replace sheet(Gang 1) format(SCLR3 NCLR3) ///
	keep(*week_int* )

	
foreach i in $depvars {
reg `i' i.week_int if gang==2  
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 2
local means_all = r(mean)
eststo `i'_2, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_2 , save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G2) sd2 append sheet(Gang 2) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==3  
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 3
local means_all = r(mean)
eststo `i'_3, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_3, save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G3) sd2 append sheet(Gang 3) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==4  
mat cons = r(table)
local cons_all = cons[1,7]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 4
local means_all = r(mean)
eststo `i'_4, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_4, save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G4) sd2 append sheet(Gang 4) format(SCLR3 NCLR3) ///
	keep(*week_int* ) 


foreach i in $depvars {
reg `i' i.week_int if gang==5  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,7]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 5
local means_all = r(mean)
eststo `i'_5, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_5, save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G5) sd2 append sheet(Gang 5) format(SCLR3 NCLR3) ///
	keep(*week_int* ) 
	
foreach i in $depvars {
reg `i' i.week_int if gang==6  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 6
local means_all = r(mean)
eststo `i'_6, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_6, save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G6) sd2 append sheet(Gang 6) format(SCLR3 NCLR3) ///
	keep(*week_int*) // BMI only 
	
foreach i in $depvars {
reg `i' i.week_int if gang==7  
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 7
local means_all = r(mean)
eststo `i'_7, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_7, save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G7) sd2 append sheet(Gang 7) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==8  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,5]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang == 8
local means_all = r(mean)
eststo `i'_8, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_8, save($output\Table1.xls) stats(N pvalue_all means_all cons_all) title(G8) sd2 append sheet(Gang 8) format(SCLR3 NCLR3) ///
	keep(*week_int* )	

**************************************************
**Table 7A:  Health and Health Behaviors Balance**
**************************************************
**Group by week balance**

use "$data\workerbyweek.dta", clear
egen worker_tag=tag(workerID)

eststo clear
foreach outcome in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `outcome' i.week_int if gang==1 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `outcome' if gang==1 & worker_tag==1
local means_all = r(mean)
eststo `outcome'_A, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_A, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G1) sd2 replace sheet(Gang 1) format(SCLR3 NCLR3) ///
	keep(*week_int )	


eststo clear
foreach i in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `i' i.week_int if gang==2 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==2 & worker_tag==1
local means_all = r(mean)
eststo `i'_9, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_9, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G2) sd2 append sheet(Gang 2) format(SCLR3 NCLR3) ///
	keep(*week_int)	

	
eststo clear	
foreach i in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `i' i.week_int if gang==3 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==3 & worker_tag==1
local means_all = r(mean)
eststo `i'_9, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_9, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G3) sd2 append sheet(Gang 3) format(SCLR3 NCLR3) ///
	keep(*week_int)	

eststo clear	
foreach i in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `i' i.week_int if gang==4 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,7]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==4 & worker_tag==1
local means_all = r(mean)
eststo `i'_9, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_9, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G4) sd2 append sheet(Gang 4) format(SCLR3 NCLR3) ///
	keep(*week_int)	
	
eststo clear	
foreach i in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `i' i.week_int if gang==5 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,7]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==5 & worker_tag==1
local means_all = r(mean)
eststo `i'_9, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_9, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G5) sd2 append sheet(Gang 5) format(SCLR3 NCLR3) ///
	keep(*week_int)	

	
eststo clear	
foreach i in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `i' i.week_int if gang==6 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==6 & worker_tag==1
local means_all = r(mean)
eststo `i'_9, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_9, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G6) sd2 append sheet(Gang 6) format(SCLR3 NCLR3) ///
	keep(*week_int)	

	
eststo clear
foreach i in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `i' i.week_int if gang==7 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==7 & worker_tag==1
local means_all = r(mean)
eststo `i'_9, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_9, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G7) sd2 append sheet(Gang 7) format(SCLR3 NCLR3) ///
	keep(*week_int )	

eststo clear	
foreach i in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `i' i.week_int if gang==8 & worker_tag==1
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,5]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==8 & worker_tag==1
local means_all = r(mean)
eststo `i'_9, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_9, save($output\Table7A.xls) stats(N pvalue_all means_all cons_all) title(G8) sd2 append sheet(Gang 8) format(SCLR3 NCLR3) ///
	keep(*week_int )	

********************************
**Table 7B - Robustness Checks**
********************************

**ROBUSTNESS CHECK 1:  Balance across study weeks among those not interviewed in the first phase of the project**	

gen week_123=.
replace week_123=1 if week_int==1 | week_int==2 | week_int==3
replace week_123=0 if week_int==4 | week_int==5 | week_int==6

eststo clear
foreach outcome in mal_pos feversincestart seek_formal seek_informal anymorbid othillsincestart {
reg `outcome' week_123 if worker_tag==1, vce( cluster gang) 
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,2]
testparm week_123
local pvalue_all=r(p)
sum `outcome' if worker_tag==1
local means_all = r(mean)
eststo `outcome'_A, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_A , save($output\Table7B.xls) stats(N pvalue_all means_all cons_all) title(Early v late) sd2 replace sheet(Early v Late Sample) format(SCLR3 NCLR3) ///
	keep(week_123 )

**ROBUSTNESS CHECK 2:  Balance across terciles of interview assignments**

gen week_12_34_56=.
replace week_12_34_56=1 if week_int==1 | week_int==2
replace week_12_34_56=2 if week_int==3 | week_int==4 
replace week_12_34_56=3 if week_int==5 | week_int==6


eststo clear
foreach outcome in mal_pos scrabbledays anymorbid seek_formal seek_informal feversincestart othillsincestart {
reg `outcome' i.week_12_34_56  if worker_tag==1, vce( cluster gang) 
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,4]
testparm i.week_12_34_56 
local pvalue_all=r(p)
sum `outcome' if worker_tag==1
local means_all = r(mean)
eststo `outcome'_A, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_A , save($output\Table7B.xls) stats(N pvalue_all means_all cons_all) title(Tercile Divided Sample) sd2 append sheet(Tercile Divided Sample) format(SCLR3 NCLR3) ///
	keep(*week_12_34_56 )

**********************************************************
**Table 2A:	Balancing tests for malaria positive workers**	
**********************************************************

use "$data\balance.dta", replace

global depvars "age ln_exp_year edyrs BMI hhsize_p rooms_p n_cattle_p n_poultry_p hhasset_m1D"
keep if mal_pos==1

eststo clear
foreach i in $depvars {
reg `i' i.week_int if gang==1  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==1
local means_all = r(mean)
eststo `i'_1, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_1 , save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G1) sd2 replace sheet(Gang 1) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
	
foreach i in $depvars {
reg `i' i.week_int if gang==2  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,5]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==2
local means_all = r(mean)
eststo `i'_2, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_2 , save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G2) sd2 append sheet(Gang 2) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==3  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==3
local means_all = r(mean)
eststo `i'_3, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_3, save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G3) sd2 append sheet(Gang 3) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==4  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==4
local means_all = r(mean)
eststo `i'_4, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_4, save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G4) sd2 append sheet(Gang 4) format(SCLR3 NCLR3) ///
	keep(*week_int* )


foreach i in $depvars {
reg `i' i.week_int if gang==5  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,5]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==5
local means_all = r(mean)
eststo `i'_5, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_5, save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G5) sd2 append sheet(Gang 5) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==6  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,5]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==6
local means_all = r(mean)
eststo `i'_6, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_6, save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G6) sd2 append sheet(Gang 6) format(SCLR3 NCLR3) ///
	keep(*week_int*)
	
foreach i in $depvars {
reg `i' i.week_int if gang==7  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==1
local means_all = r(mean)
eststo `i'_7, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_7, save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G7) sd2 append sheet(Gang 7) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==8  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,5]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==7
local means_all = r(mean)
eststo `i'_8, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_8, save($output\Table2A.xls) stats(N pvalue_all means_all cons_all) title(G8) sd2 append sheet(Gang 8) format(SCLR3 NCLR3) ///
	keep(*week_int* )	
	
**********************************************************
**Table 2B:	Balancing tests for malaria negative workers**	
**********************************************************

use "$data\balance.dta", replace

global depvars "age ln_exp_year edyrs BMI hhsize_p rooms_p n_cattle_p n_poultry_p hhasset_m1D"
keep if mal_pos==0

eststo clear
foreach i in $depvars {
reg `i' i.week_int if gang==1  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==1
local means_all = r(mean)
eststo `i'_1, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_1 , save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G1) sd2 replace sheet(Gang 1) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
	
foreach i in $depvars {
reg `i' i.week_int if gang==2  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==2
local means_all = r(mean)
eststo `i'_2, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_2 , save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G2) sd2 append sheet(Gang 2) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==3  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==3
local means_all = r(mean)
eststo `i'_3, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_3, save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G3) sd2 append sheet(Gang 3) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==4  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==4
local means_all = r(mean)
eststo `i'_4, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_4, save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G4) sd2 append sheet(Gang 4) format(SCLR3 NCLR3) ///
	keep(*week_int* )


foreach i in $depvars {
reg `i' i.week_int if gang==5  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,7]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==5
local means_all = r(mean)
eststo `i'_5, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_5, save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G5) sd2 append sheet(Gang 5) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==6  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==6
local means_all = r(mean)
eststo `i'_6, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_6, save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G6) sd2 append sheet(Gang 6) format(SCLR3 NCLR3) ///
	keep(*week_int*)
	
foreach i in $depvars {
reg `i' i.week_int if gang==7  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,6]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==7
local means_all = r(mean)
eststo `i'_7, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}
xml_tab  *_7, save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G7) sd2 append sheet(Gang 7) format(SCLR3 NCLR3) ///
	keep(*week_int* )
	
foreach i in $depvars {
reg `i' i.week_int if gang==8  
mat list r(table)
mat cons = r(table)
local cons_all = cons[1,4]
testparm i.week_int
local pvalue_all=r(p)
sum `i' if gang==8
local means_all = r(mean)
eststo `i'_8, addscalars(pvalue_all `pvalue_all' cons_all `cons_all' means_all `means_all')
}	
xml_tab  *_8, save($output\Table2B.xls) stats(N pvalue_all means_all cons_all) title(G8) sd2 append sheet(Gang 8) format(SCLR3 NCLR3) ///
	keep(*week_int* )	

***************************
** Table 4:  ITT Results **
***************************
** Daily ITT results **
	
use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

replace scrabbledays=. if attendance==0 | attendance==. 
rename scrabbledays cond_scrabble

foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week gains
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9, vce(cluster workerID) 
estimates store `outcome'_ITTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16, vce(cluster workerID)
estimates store `outcome'_ITTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23, vce(cluster workerID)
estimates store `outcome'_ITTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30, vce(cluster workerID)
estimates store `outcome'_ITTdayFE4

}


foreach outcome in attendance lnearnings cond_scrabble{

xml_tab `outcome'_ITTdayFE1 `outcome'_ITTdayFE2 `outcome'_ITTdayFE3 `outcome'_ITTdayFE4, ///
save($output/`outcome'_T4_within.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)

}


** Weekly ITT results **
*now looking at the one week gains from treatment
local i = 2
while `i' <= 7 {
use "$data\workerbyweek.dta", clear
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	sort workerID
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet1

}


*now looking at the two week gains from treatment
local i = 2
while `i' <= 6 {
use "$data\workerbyweek.dta", clear
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & ///
(week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet2

}


*now looking at the three week gains from treatment
local i = 2
while `i' <= 5 {
use "$data\workerbyweek.dta", clear
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & ///
(week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {


di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet3

}


*now looking at the four week gains from treatment
local i = 2
while `i' <= 4 {
use "$data\workerbyweek.dta", clear
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & (week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_T4_bet4

}


foreach outcome in lndays lnearnings lnwage scrabbleratio {

xml_tab `outcome'_T4_bet1 `outcome'_T4_bet2 `outcome'_T4_bet3 `outcome'_T4_bet4, ///
	save($output/`outcome'_T4_bet.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T* )

}

estimates clear

**************************
** Table 5: TOT results **
**************************
** Daily TOT results **

use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

replace scrabbledays=. if attendance==0 | attendance==. 
rename scrabbledays cond_scrabble

foreach outcome in attendance lnearnings cond_scrabble {

di "`outcome'"

** One week reference
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 1, vce(cluster workerID)
estimates store `outcome'_TOTdayFE4

}

foreach outcome in attendance lnearnings cond_scrabble {

xml_tab `outcome'_TOTdayFE1 `outcome'_TOTdayFE2 `outcome'_TOTdayFE3 `outcome'_TOTdayFE4, ///
save($output/`outcome'_T5_within.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)
}

**I don't think we need this section any more but am leaving it in for now. 
**Appendix table on dynamic scrabble days**

*generating the terciles of interview period;
gen Ter1 = (interview_date >= 18281 & interview_date <= 18292)
gen Ter2 = (interview_date >= 18293 & interview_date <= 18304) 
gen Ter3 = (interview_date >= 18305 & interview_date <= 18316)

*generating various interaction terms;
gen Ter1T = Ter1*T
gen Ter2T = Ter2*T
gen Ter3T = Ter3*T

gen Ter1M = Ter1*mal_pos
gen Ter2M = Ter2*mal_pos
gen Ter3M = Ter3*mal_pos

gen Ter1TM = Ter1*T*mal_pos
gen Ter2TM = Ter2*T*mal_pos
gen Ter3TM = Ter3*T*mal_pos

gen TM = T*mal_pos


** Weekly TOT results **
*now looking at the one week gains for those who tested positive for malaria
local i = 2
while `i' <= 7 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	sort workerID
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT1week

}


*now looking at the two week gains from treatment
local i = 2
while `i' <= 6 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT2week

}


*now looking at the three week gains from treatment
local i = 2
while `i' <= 5 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT3week

}


*now looking at the four week gains from treatment
local i = 2
while `i' <= 4 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 1
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & ///
(week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TOT4week

}


foreach outcome in lndays lnearnings lnwage scrabbleratio {

xml_tab `outcome'_TOT1week `outcome'_TOT2week `outcome'_TOT3week `outcome'_TOT4week, ///
	save($output/`outcome'_T5_bet.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

***************************
** Table 6: TmUT results **
***************************
** Daily TmUT results **

use "$data\dailydata.dta", clear

drop if days_since_int >= 0 & days_since_int <= 2 // drop days between interview and receipt of diagnosis

replace scrabbledays=. if attendance==0 | attendance==. 
rename scrabbledays cond_scrabble

foreach outcome in attendance lnearnings cond_scrabble {

** One week reference 
reg `outcome' T i.workerID if days_since_int >= -7 & days_since_int <= 9 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE1

** Two week reference
reg `outcome' T i.workerID if days_since_int >= -14 & days_since_int <= 16 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE2

** Three week reference
reg `outcome' T i.workerID if days_since_int >= -21 & days_since_int <= 23 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE3

** Four week reference
reg `outcome' T i.workerID if days_since_int >= -28 & days_since_int <= 30 & mal_pos == 0, vce(cluster workerID)
estimates store `outcome'_TmUTdayFE4

}

foreach outcome in attendance lnearnings cond_scrabble {

xml_tab `outcome'_TmUTdayFE1 `outcome'_TmUTdayFE2 `outcome'_TmUTdayFE3 `outcome'_TmUTdayFE4, ///
save($output/`outcome'_T6_within.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T)
}


** Weekly TmUT results **
*now looking at the one week gains for those who tested negative for malaria
local i = 2
while `i' <= 7 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w1 = 1 if (week_int == `i'-1)
replace T_w1 = 0 if (week_int == `i'+1)
gen studyweeks = 1 if (week_earn == `i'  & (week_int == `i' - 1 | week_int == `i' + 1))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT1week

}

*now looking at the two week gains from treatment

local i = 2
while `i' <= 6 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w12 = 1 if (week_int == `i'-1)
replace T_w12 = 0 if (week_int == `i'+2)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1) & (week_int == `i' - 1 | week_int == `i' + 2))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w12 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w12 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT2week

}

*now looking at the three week gains from treatment

local i = 2
while `i' <= 5 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w123 = 1 if (week_int == `i'- 1)
replace T_w123 = 0 if (week_int == `i'+ 3)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2) & (week_int == `i' - 1 | week_int == `i' + 3))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w123 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w123 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT3week

}
	
*now looking at the four week gains from treatment

local i = 2
while `i' <= 4 {
use "$data\workerbyweek.dta", clear
keep if mal_pos == 0
gen T_w1234 = 1 if (week_int == `i'- 1)
replace T_w1234 = 0 if (week_int == `i'+ 4)
gen studyweeks = 1 if ((week_earn == `i' | week_earn == `i' + 1 | week_earn == `i' + 2 | week_earn == `i' + 3) & (week_int == `i' - 1 | week_int == `i' + 4))
keep if studyweeks == 1
sleep 1000
if `i' == 2 {
	save "$data\temp.dta", replace
	}
if `i' > 2 {
	append using "$data\temp.dta"
	save "$data\temp.dta", replace
	}
local i = `i' + 1
}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

di "`outcome'"
reg `outcome' T_w1234 if studyweeks == 1, vce(cluster workerXround)
reg `outcome' T_w1234 gangBYweek* if studyweeks == 1, vce(cluster workerXround)
estimates store `outcome'_TMUT4week

}

foreach outcome in lndays lnearnings lnwage scrabbleratio {

xml_tab `outcome'_TMUT1week `outcome'_TMUT2week `outcome'_TMUT3week `outcome'_TMUT4week, ///
	save($output/`outcome'_T6_bet.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep(T*)

}

estimates clear

*****************************
// TABLE 8 - PLACEBO TESTS //
*****************************

use "$data\dailydata.dta", clear
keep if week_earn<7 // study weeks only

** Looking at 7 days before med date, 14 days before med date, 21 days before med date **
replace attendance=0 if total_amount==0  // there are 59 workers with attendance==1 but total_amount==0

** 7, 14, 21 days BEFORE actual treatment date
gen days_since_int_7 = earn_date - (interview_date-7) // note that I am denoting days before with an underscore
gen days_since_int_14 = earn_date - (interview_date-14)
gen days_since_int_21 = earn_date - (interview_date-21)

gen T_7 = 1 if days_since_int_7 - 2 > 0
replace T_7 = 0 if days_since_int_7 - 2 <= 0

gen T_14 = 1 if days_since_int_14 - 2 > 0
replace T_14 = 0 if days_since_int_14 - 2 <= 0

gen T_21 = 1 if days_since_int_21 - 2 > 0
replace T_21 = 0 if days_since_int_21 - 2 <= 0


** No worker FE **
** "Treatment" date BEFORE actual treatment date
foreach i in _7 _14 _21  {
foreach outcome in attendance lnearnings {

** One week gains
reg `outcome' T`i' if days_since_int`i' >= -7 & days_since_int`i' <= 9, vce(cluster workerID) 
estimates store `outcome'`i'_ITTdayNOFE1

** Two week reference
reg `outcome' T`i' if days_since_int`i' >= -14 & days_since_int`i' <= 16, vce(cluster workerID)
estimates store `outcome'`i'_ITTdayNOFE2

** Three week reference
reg `outcome' T`i' if days_since_int`i' >= -21 & days_since_int`i' <= 23, vce(cluster workerID)
estimates store `outcome'`i'_ITTdayNOFE3

** Four week reference
reg `outcome' T`i' if days_since_int`i' >= -28 & days_since_int`i' <= 30, vce(cluster workerID)
estimates store `outcome'`i'_ITTdayNOFE4

}
}

foreach i in _7 _14 _21  {
foreach outcome in attendance lnearnings {

xml_tab `outcome'`i'_ITTdayNOFE1 `outcome'`i'_ITTdayNOFE2 `outcome'`i'_ITTdayNOFE3 `outcome'`i'_ITTdayNOFE4, ///
save($output/`outcome'_T8_`i'before_ITT_NOFE.xls) replace stats(N pvalue_all) format(SCLR3 NCLR3) keep (T`i')

}
}



erase "$data\temp.dta"

timer off 1
timer list 1

